package com.example.spark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spark.dao.PublicizeDao;
import com.example.spark.entity.Publicize;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.PublicizeService;
import com.example.spark.service.minio.MinioCloudStorageService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

import static com.baomidou.mybatisplus.core.toolkit.IdWorker.getId;

@Service
public class PublicizeServiceImpl extends ServiceImpl<PublicizeDao, Publicize> implements PublicizeService {

    @Resource
    MinioCloudStorageService minIoService;

    @Autowired
    private PublicizeDao publicizeDao;


    // 新增招新宣传信息
    public CommonResult addPublicize(Publicize publicize) {
        LambdaQueryWrapper<Publicize> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(publicize.getName() != null,Publicize::getName,publicize.getName());
        Publicize publicizes = publicizeDao.selectOne(lambdaQueryWrapper);
        if (publicizes == null) {
            publicizeDao.insert(publicize);
            return new CommonResult(200,"新增成功");
        } else {
            return new CommonResult(200,"数据库已有该数据无法继续新增");
        }
    }

    // 修改活动信息
    public CommonResult updatePublicize(@RequestBody Publicize publicize)  {
        if (publicize.getId() == null) {
            return new CommonResult(200,"缺少主要条件，无法进行修改");
        } else {
            publicizeDao.updateById(publicize);
            return new CommonResult(200,"修改成功");
        }

    }

    // 修改图片信息
    public CommonResult updatePhoto(MultipartFile photo, Long id) throws IOException {
        Publicize publicize = publicizeDao.selectById(id);
        String extension = FilenameUtils.getExtension(photo.getOriginalFilename());
        String url = minIoService.uploadPhotoSuffix(photo.getBytes(), extension, publicize.getName());
        Publicize publicize1 = new Publicize();
        publicize1.setId(id);
        publicize1.setPhoto(url);
        publicizeDao.updateById(publicize1);
        return new CommonResult(200,"图片操作成功");
    }


    // 查询招新宣传信息
    public CommonResult getPublicize(String name) {
        LambdaQueryWrapper<Publicize> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(Publicize::getName,name);
        Publicize publicize = publicizeDao.selectOne(lambdaQueryWrapper);
        CommonResult commonResult = new CommonResult(200, "success", publicize);
        return commonResult;
    }

}
